const Controller=require('egg').Controller
const utils=require('../utils/utils')
class UserController extends Controller {
    async info(){
        const {ctx}= this
        ctx.body={
            code:1,
            data:ctx.userInfo
        }
    }
    async message_code(){
        const {ctx}=this
        const {phone}=ctx.request.query
        const code =utils.randomCode(6)
        if(phone){
            ctx.body={
                code:1,
                message:`已发送验证码${code}到用户${phone}`
            }
        }else{
            ctx.throw(422,'手机号未注册')
        }
        
    }
     async login(){
         const {ctx} =this
         const {phone,msg_code} =ctx.request.body;
         const rule={
             phone:{type:'String',required:true},
             msg_code:{type:'String',required:true},
         }
         ctx.validate(rule,ctx.request.body)
         console.log(phone,msg_code)
         const user =await ctx.service.user.find_user(phone)
         if(user){
             ctx.body={
                 code:1,
                 message:'用户登陆成功'
             }
         }else{
            ctx.throw(422,'手机号未注册')
         }
        } 
        async detail(){
            let {ctx,service}=this;
            let {id}=ctx.params;
            console.log(id)
            if(id){
            try{
            let data=await service.user.detail(id);
            ctx.body={
              code:1,
              msg:data
            }
            }catch(e){
            ctx.body={
              code:0,
              msg:e
            }
            }
            }else{
              ctx.body={
                code:2,
                msg:"参数错误"
              }
            }
          }
          async createAddress(){
              const {ctx} =this
              const body=ctx.request.body
              try{
                  await ctx.service.user.create_address({
                      ...body,
                      user_id:ctx.userInfo.id
                  })
                  ctx.body={
                      code:1,
                      message:'添加成功'
                  }
              }catch(e){
                  ctx.throw(422,'添加失败')
              }
          }
          async selectAddress(){
            const {ctx} =this
            const {user_id}=ctx.request.body
            try{
                await ctx.service.user.select_address({
                   user_id
                })
                ctx.body={
                    code:1,
                    message:'查找成功'
                }
            }catch(e){
                ctx.throw(422,'查找失败')
            }
        }
        async getAddress(){
            const {ctx} =this
            const {id}=ctx.request.body
            try{
                await ctx.service.user.get_address({
                   id
                })
                ctx.body={
                    code:1,
                    message:'添加成功'
                }
            }catch(e){
                ctx.throw(422,'添加失败')
            }
        }
}
module.exports=UserController